﻿
function FinanceCheckOut() {
    var url = baseUrl + '/BankData/FinanceCheckOut';
    var ids = [];
    $('input[name=selected]:checked').each(function (i, item) {
        ids.push($(item).val());
    });
    if (ids.length > 0) {
        var data = { ids: ids.join(',') };
        ShowDialogFormEdit(url, data, "财务签出");
    } else {
        alert('至少选择一个账单');
    }
}

function CheckIn(id) {
    var url = baseUrl + '/BankData/CheckIn';
    $.post(url, { id: id }, function (r) {
        if (r.Result) {
            RefreshSearchContent();
        }
        MsgAlert(r.MSG);
    });
}

function getCustomer() {
    var url = baseUrl + '/Customer/GetCustomer';
    ShowSelect(url, function (r) {
        $('#CustomerCode').val(r.attr('value').replace(/\s/g, ''));
        $('#CustomerName').val(r.attr('text').replace(/\s/g, ''))
    });
}

function searchCustomer() {
    var url = baseUrl + '/Customer/SearchCustomer';
    var data = { keyword: $('#Keyword', "#dvcustomer").val() };
    GetAJAX(url, data, function (r) {
        $('#customerList').html(r);
    });
}

function DeptChange() {
    var url = baseUrl + "/UserInfo/SearchEmployee";
    var data = { dept: $('#dept', "#dvcustomer").val() };
    GetAJAX(url, data, function (r) {
        $('#customerList').html(r);
    });
}

function BandDataReport() {
    var data = $('form').serialize();
    var url = baseUrl + '/BankData/ReportToDownload';
    url+="?"+data;
    window.open(url, target = 'blank');
}
function ApymentReport() {
    var data = $('form').serialize();
    var url = baseUrl + '/PaymentApply/ReportToDownload';
    url += "?" + data;
    window.open(url, target = 'blank');
}

function ConfirmOrders() {
    var url = baseUrl + "/ReleaseOrder/ConfirmOrders";
    var ids = [];
    $('input[name=selected]:checked').each(function (i, item) {
        ids.push($(item).val());
    });
    var data = { ids: ids.join(',') };
    $.post(url, data, function (r) {
        if (r.Result) {
            RefreshSearchContent();
        }
        MsgAlert(r.MSG);
    });
}

function RejectOrders() {
    var url = baseUrl + "/ReleaseOrder/RejectOrders";
    var ids = [];
    $('input[name=selected]:checked').each(function (i, item) {
        ids.push($(item).val());
    });
    var data = { ids: ids.join(',') };
    $.post(url, data, function (r) {
        if (r.Result) {
            RefreshSearchContent();
        }
        MsgAlert(r.MSG);
    });
}

function SaveIPBankData() {
    var url = baseUrl + "/BankData/SaveIPBankData";
    var data = { batch: $('#batchID').val() };
    $.post(url, data, function (r) {
        if (r.Result) {
            clearIPBankData();
            alert(r.MSG);
        } else {
            alert(r.MSG);
        }
    });
}

function CancelIPBankData() {
    var url = baseUrl + "/BankData/CancelIPBankData";
    var data = { batch: $('#batchID').val() };
    $.post(url, data, function (r) {
        if (r.Result) {
            clearIPBankData();
            alert(r.MSG);
        } else {
            alert(r.MSG);
        }
    });
}

function clearIPBankData() {
    $('#batchID').val('');
    $('#importData').html('');
}

function ShowImportSapResult() {
    var url = baseUrl + "/BankData/ImportSapResult";
    GetAJAX(url,{rand:Math.random()}, function (r) {
        var dialog = bootbox.dialog(r, [{
            "label": "取消",
            "icon": "icon-remove",
            "callback": function () { }
        }, {
            "label": "导入",
            "icon": 'icon-ok',
            "class": "btn-primary",
            "callback": function (ele) {
                var data = { FileName: $('#FileName').val() };
                $.post(url, data, function (result) {
                    if (result.Result) {
                        $(ele).parents('.modal').modal('hide');

                        //刷新
                        RefreshSearchContent();

                    } else
                        alert(result.MSG);
                });
                return false;
            }
        }],
        { header: "导入SAP结果", classes: "winModal" });
    });
}

function AgreePaymentApply(ele, id) {
    var data = { id: id };
    var url = baseUrl + "/PaymentApply/Agree";
    $.post(url, data, function (r) {
        if (r.Result) {
            $(ele).parents('.modal').modal('hide');
        } else {
            alert(r.MSG);
        }
    });
}

function RejectPaymentApply(ele, id) {
    var data = { id: id };
    var url = baseUrl + "/PaymentApply/Reject";
    $.post(url, data, function (r) {
        if (r.Result) {
            $(ele).parents('.modal').modal('hide');
        } else {
            alert(r.MSG);
        }
    });
}

function CancelCheckRecordAlert(el) {
    var  text = "确定撤销签出吗？\n撤销签出会删除签出记录、认款、退款和放单记录";
    if (confirm(text)) {
        var url = $(el).attr('href');
        $.post(url, function (r) {
            if (r.Result) {
                RefreshSearchContent();
            } else
                alert(r.MSG);
        });
    }
}

function DetailsApproveDispute(el) {
    var text = $.trim($(el).text());
    text = "确定" + text + "吗？";
    if (confirm(text)) {
        var url = $(el).attr('href');
        $.post(url, function (r) {
            if (r.Result) {
                var targetUrl = $('#disputeListBody').attr('targeturl');
                $.get(targetUrl, function (result) {
                    $('#disputeListBody').html(result);
                });
            } else
                alert(r.MSG);
        });
    }
}


function ApproveDispute(el) {
    var url = $(el).attr('href');
    var title = $(el).text();
    GetAJAX(url, { rand: Math.random() }, function (r) {
        bootbox.dialog(r, [
        {
            "label": "取消",
            "icon": "icon-remove",
            "callback": function () { }
        },
        {
            "label": "拒绝争议",
            "icon": 'icon-remove',
            "callback": function (ele) {
                var Rejecturl = baseUrl + '/DisputeApply/Reject/';
                var modal = $(ele).parents('.modal');
                var id = modal.find('#Id').val();
                $.post(Rejecturl, { id: id }, function (result) {
                    if (result.Result) {
                        RefreshSearchContent();
                        modal.modal('hide');
                    } else {
                        alert(result.MSG);
                    }
                });
                return false;
            }
        }, {
            "label": "同意争议",
            "class": "btn-primary",
            "icon": 'icon-ok',
            "callback": function (ele) {

                $.post(url, { rand: Math.random() }, function (result) {
                    if (result.Result) {
                        RefreshSearchContent();
                        $(ele).parents('.modal').modal('hide');
                    } else {
                        alert(result.MSG);
                    }
                });
                return false;
            }
        },
        {
            "label": "撤销签出",
            "icon": 'icon-ok',
            "callback": function (ele) {
                var text = "确定撤销签出吗？\n撤销签出会删除签出记录、认款、退款和放单记录";
                if (confirm(text)) {
                    var CancelCheckurl = baseUrl + '/BankData/CancelCheckRecord/';
                    var modal = $(ele).parents('.modal');
                    var id = modal.find('#BankDataId').val();
                    $.post(CancelCheckurl, { id: id }, function (result) {
                        if (result.Result) {
                            RefreshSearchContent();
                            modal.modal('hide');
                        } else {
                            alert(result.MSG);
                        }
                    });
                }
                return false;
            }
        }],
        { header: title, classes: "winModal" });
        BindFormEvent();
    });
}

function deleteCheckRecord(el) {
    $(el).parents('fieldset:first').remove();
}